package com.amigo.online.common.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.json.JSONException;
import org.springframework.context.i18n.LocaleContextHolder;

import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;

public class SmsUtil {

	public static Map<String,Integer> SmsCnMap = new HashMap<String,Integer>() {

		private static final long serialVersionUID = 6701306821592948143L;

		{
			put("checkcode", 153960);
		}
	};
	public static Map<String,Integer> SmsEnMap = new HashMap<String,Integer>() {

		private static final long serialVersionUID = 8733761162211025228L;

		{
			put("checkcode", 245436);
		}
	};
	public static Map<String,Integer> SmsEsMap = new HashMap<String,Integer>() {

		private static final long serialVersionUID = 8733761162211025228L;

		{
			put("checkcode", 245436);
		}
	};
	public static Map<String,Integer> SmsPtMap = new HashMap<String,Integer>() {

		private static final long serialVersionUID = 8733761162211025228L;

		{
			put("checkcode", 245436);
		}
	};
	
	public static Integer Map(String templateKey) {
		Locale locale = LocaleContextHolder.getLocale();
		Map<String,Integer> res=SmsPtMap;//默认葡萄牙语
		switch(locale.getLanguage().split("_")[0].toLowerCase()) {
		case "zh":
			res = SmsCnMap;
			break;
		case "en":
			res = SmsEnMap;
			break;
		case "pt":
			res = SmsPtMap;
			break;
		case "es":
			res = SmsEsMap;
			break;
		default:
			res = SmsPtMap;
		}
		return res.get(templateKey);
	}

	// 短信应用SDK AppID
	static int appid = 1400110130; // 1400开头

	// 短信应用SDK AppKey
	static String appkey = "5a81910d4a6c1efda352a47466c29c51";

	// 需要发送短信的手机号码
	// static String[] phoneNumbers = {"31993688888"};//00 55 (31) 99368-8888 55
	// (31) 97338-8999 （31）993977778

	// 短信模板ID，需要在短信应用中申请
	// static int templateId = 142047; // NOTE:
	// 这里的模板ID`7839`只是一个示例，真实的模板ID需要在短信控制台中申请

	// 签名
	// static String smsSign = "amigocomer"; // NOTE:
	// 这里的签名"腾讯云"只是一个示例，真实的签名需要在短信控制台中申请，另外签名参数使用的是`签名内容`，而不是`签名ID`

	public static Boolean enabled = true;

	public static int send(String nationCode, String phoneNumber, String templateKey, String[] params) {
		SmsSingleSenderResult result = null;
		if (enabled) {
			try {
				SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
//				result = ssender.sendWithParam(nationCode, phoneNumber, Map(templateKey), params, "", "", ""); // 签名参数未提供或者为空时，会使用默认签名发送短信
				Map<String,Integer> map=null;
				if(nationCode.contains("+")) {
					nationCode=nationCode.replace("+", "");
				}
				if("86".equals(nationCode)) {
					map=SmsCnMap;
				}else {
					map=SmsPtMap;
				}
				result = ssender.sendWithParam(nationCode, phoneNumber, map.get(templateKey), params, "", "", ""); 
				System.out.print(result);
			} catch (HTTPException e) {
				// HTTP响应码错误
				e.printStackTrace();
			} catch (JSONException e) {
				// json解析错误
				e.printStackTrace();
			} catch (IOException e) {
				// 网络IO错误
				e.printStackTrace();
			}
			return result.result;
		} else {
			return 9999;
		}

	}

}
